
options ls=100 ps=1000 nofmterr;

libname new '/data/Medicare_P01_2009/data/HEDIS/sasdata';


data a0; set new.hedis2007;

     if org_id='HS524' or org_id='HN524' then plan_type='HMO/HMOPOS';

     if denom_flag=1;
     if state_code=' ' or state_code='00' or state_code>"53" or state_code in ("40","48") then delete;

     if age>=64;  /* Age at end of year */

     if plan_type in ('HMO/HMOPOS','Local PPO');


     if sex=1 or age>69 then bcs_d_52_69=0; 
     if age>75 then cdc_d1=0; 

     if age>65 and totmonth<12 then delete;

     death_dt=bene_death_dt;
     if (bene_valid_death_dt_sw='V' or death_dt>0) then death1=1; else death1=0;

     deathyear=year(death_dt);
     deathmon =month(death_dt);

     if death1=1 and
       (deathyear=2007 or
       (deathyear=2008 and (deathmon>0 and deathmon<3)))
     then delete ;

     if org_id in ('H4009','H5732','H5760','H5887') then delete;    


     **-- Recode 9 or 999 to 0s --**;

     array rate bcs_d_52_69 bcs_n_52_69 cmc_d cmc_n1 cdc_d1 cdc_n1 cdc_d4 cdc_n4 cdc_d5 cdc_n5 gso_d gso_n;  
     do over rate; 
        if rate=9 then rate=0; 
     end;

     *-- cleaning BCS --*;

     if sex=1 or age>69 then bcs_d_52_69=.;
     if bcs_d_52_69=0 or bcs_d_52_69=. then bcs_n_52_69=.;

     *-- cleaning CMC and GSO --*;

     if cmc_d=0 then cmc_n1=.;
     if gso_d=0 then gso_n=.;

     *-- cleaning CDC ---*;

     if age>75 then do; cdc_d1=.; cdc_d4=.; cdc_d5=.; end;
     if cdc_d1=0 or cdc_d1=. then cdc_n1=.;
     if cdc_d4=0 or cdc_d4=. then cdc_n4=.;
     if cdc_d5=0 or cdc_d5=. then cdc_n5=.;

     *-- Clean up FSP variables --*;

        array fpsuti  fsp_n_cabg  fsp_n_ptca  fsp_n_cc      fsp_n_ce    fsp_n_rff   fsp_n_thr  fsp_n_tkr  fsp_n_peli
                      fsp_n_och   fsp_n_cch   fsp_n_ah      fsp_n_vh    fsp_n_pros  fsp_n_mast fsp_n_lump fsp_n_bs 
                      ipu_t_acdi  ipu_t_acsdi ipu_t_acmedi  ipu_t_acdy  ipu_t_acsdy ipu_t_acmedy
                      amb_t_ov    amb_t_ed    amb_t_asp     amb_t_obsrd ;
        do over fpsuti;
           if fpsuti=999 then fpsuti=0;
        end;

        **-- Clean up Gender Related Miscoding - very low occurance --**;

        if sex=1 then do; fsp_n_ah=.; fsp_n_vh=.;  fsp_n_mast=.; fsp_n_lump=.; end;  /** female only**/
        if sex=2 then fsp_n_pros=.;   /** male only**/


       **-- CAP measures  John and Bruce email 6-4-2010 --**;

       if fsp_n_cabg>3 then fsp_n_cabg=3;   
       if fsp_n_ptca>4 then fsp_n_ptca=4;   
       if fsp_n_cc>4   then fsp_n_cc=4;   
     
       array fps2 fsp_n_ce   fsp_n_rff   fsp_n_thr   fsp_n_tkr   fsp_n_peli    fsp_n_och   fsp_n_cch  
                  fsp_n_ah   fsp_n_vh    fsp_n_pros  fsp_n_mast  fsp_n_lump    fsp_n_bs ;
       do over fps2; 
          if fps2>2 then fps2=2;  
       end;
 
       if ipu_t_acdi > 12 then ipu_t_acdi = 12 ;     * Total discharges set max 1 per month *;
       if ipu_t_acsdi > 12 then ipu_t_acsdi = 12 ;   * Surgery discharges *; 
       if ipu_t_acmedi > 12 then ipu_t_acmedi = 12 ; * Medical discharges *;

       if ipu_t_acdy > 365 then ipu_t_acdy = 365 ;  *Maximum possible in 12 months *;
       if ipu_t_acsdy > 365 then ipu_t_acsdy = 365 ;
       if ipu_t_acmedy > 365 then ipu_t_acmedy = 365 ;

       if amb_t_ov > 50 then amb_t_ov = 50;  * Set Total Outpatient Visits maximum 1 per week *;
       if amb_t_ed > 12 then amb_t_ed = 12;  * maximum 1 per month *;
       if amb_t_asp > 12 then amb_t_asp = 12;
       if amb_t_obsrd > 12 then amb_t_obsrd = 12;


     ********************************************************************;
     ** Rename All Variables and Will Keep Same Var Name for All Years **;
     ********************************************************************;

     bcs_d = bcs_d_52_69 ;   label bcs_d  = 'Denom for Breast Cancer Screening 65-69';
     bcs_n = bcs_n_52_69 ;   label bcs_n  = 'Numer for Breast Cancer Screening 65-69';

     cdc_d1 = cdc_d1;  label cdc_d1 = 'Denom for Diabetes: HbA1cTesting';
     cdc_d3 = cdc_d4;  label cdc_d3 = 'Denom for Diabetes: EyeExam';
     cdc_d4 = cdc_d5;  label cdc_d4 = 'Denom for Diabetes: LCL-C Screening';

     cdc_n1 = cdc_n1;  label cdc_n1 = 'Numer for Diabetes: HbA1cTesting';
     cdc_n3 = cdc_n4;  label cdc_n3 = 'Numer for Diabetes: EyeExam';
     cdc_n4 = cdc_n5;  label cdc_n4 = 'Numer for Diabetes: LCL-C Screening';

     n_cabg = fsp_n_cabg ; label n_cabg='CABG';
     n_ptca = fsp_n_ptca ; label n_ptca='PTCA';
     n_cc   = fsp_n_cc ;   label n_cc  ='CardiacCathetherization';
     n_ce   = fsp_n_ce ;   label n_ce  ='CardiacEnarterectomy';
     n_rff  = fsp_n_rff ;  label n_rff ='ReductionFemurFracture';
     n_thr  = fsp_n_thr ;  label n_thr ='TotalHipReplacement';
     n_tkr  = fsp_n_tkr ;  label n_tkr ='TotalKneereplacement';
     n_xli  = fsp_n_peli;  label n_xli ='ExcisionLargeIntest';
     n_oc   = fsp_n_och ;  label n_oc  ='OpenCholecystectomy';
     n_clc  = fsp_n_cch;   label n_clc ='ClosedCholecstectomy';
     n_ah   = fsp_n_ah ;   label n_ah  ='AbdominalHysterectomy';
     n_vh   = fsp_n_vh ;   label n_vh  ='VaginalHysterectomy';
     n_p    = fsp_n_pros;  label n_p   ='Prostatectomy';
     n_mast = fsp_n_mast;  label n_mast='Masterctomy' ;
     n_lump = fsp_n_lump;  label n_lump='Lumpectomy' ;
     n_bs   = fsp_n_bs;    label n_bs  ='BackSurgery';

     iptotdis   = ipu_t_acdi;   label iptotdis ='InpatTotalDischarges';
     ipsurdis   = ipu_t_acsdi;  label ipsurdis ='InpatSurgeryDischarges';
     ipmeddis   = ipu_t_acmedi; label ipmeddis ='InpatMedicineDischarges';

     iptotdays  = ipu_t_acdy;   label iptotdays ='InpatTotalDays';
     ipsurdays  = ipu_t_acsdy;  label ipsurdays ='InpatSurgeryDays';
     ipmeddays  = ipu_t_acmedy; label ipmeddays ='InpatMedicineDays';

     amb_opv =  amb_t_ov;    label amb_opv ='OutPatientVisits';
     amb_erv =  amb_t_ed;    label amb_erv ='EmergencyRoomVisits';
     amb_asp =  amb_t_asp;   label amb_asp ='AbmSugeryProcedures';
     amb_ors =  amb_t_obsrd; label amb_ors ='ObservationRoomStays';


 data a; set a0;

     array items     bcs_d bcs_n cmc_d cmc_n1 cdc_d1 cdc_d3 cdc_d4 cdc_n1 cdc_n3 cdc_n4 gso_d gso_n
                     n_cabg n_ptca n_cc n_ce n_rff n_thr n_tkr n_xli n_oc n_clc n_ah n_vh n_p n_mast n_lump n_bs
                     iptotdis ipsurdis ipmeddis iptotdays ipsurdays ipmeddays amb_opv amb_erv amb_asp amb_ors;
     do over items; if items>0 then items=1; end;

     %let all_vars= bcs_n    cmc_n1   cdc_n1   cdc_n3    cdc_n4   gso_n
                    n_cabg   n_ptca   n_cc     n_ce      n_rff    n_thr
                    n_tkr    n_xli    n_oc     n_clc     n_ah     n_vh n_p n_mast n_lump n_bs
                    iptotdis ipsurdis ipmeddis iptotdays ipsurdays ipmeddays
                    amb_opv  amb_erv  amb_asp  amb_ors ;

     proc means data=a mean n nmiss nway;
               var &all_vars;
               output out=cr0
                      mean=non0_bcs    non0_cmc   non0_cdc1   non0_cdc3   non0_cdc4   non0_gso
                           non0_cabg   non0_ptca  non0_cc     non0_ce     non0_rff    non0_thr
                           non0_tkr    non0_xli   non0_oc     non0_clc    non0_ah     non0_vh    non0_p  non0_mast non0_lump non0_bs
                           non0_tdis   non0_sdis  non0_mdis   non0_tday   non0_sday   non0_mday
                           non0_opv    non0_erv   non0_asp    non0_ors 
                      n=   totn_bcs    totn_cmc   totn_cdc1   totn_cdc3   totn_cdc4   totn_gso
                           totn_cabg   totn_ptca  totn_cc     totn_ce     totn_rff    totn_thr
                           totn_tkr    totn_xli   totn_oc     totn_clc    totn_ah     totn_vh    totn_p  totn_mast totn_lump totn_bs
                           totn_tdis   totn_sdis  totn_mdis   totn_tday   totn_sday   totn_mday
                           totn_opv    totn_erv   totn_asp    totn_ors ;


     proc means data=a mean noprint nway;
               class org_id;
               var &all_vars;
               output out=cr1
                      mean=hnon0_bcs    hnon0_cmc   hnon0_cdc1   hnon0_cdc3   hnon0_cdc4   hnon0_gso
                           hnon0_cabg   hnon0_ptca  hnon0_cc     hnon0_ce     hnon0_rff    hnon0_thr
                           hnon0_tkr    hnon0_xli   hnon0_oc     hnon0_clc    hnon0_ah     hnon0_vh
                           hnon0_p      hnon0_mast  hnon0_lump   hnon0_bs
                           hnon0_tdis   hnon0_sdis  hnon0_mdis   hnon0_tday   hnon0_sday   hnon0_mday
                           hnon0_opv    hnon0_erv   hnon0_asp    hnon0_ors
                      n=   hn_bcs    hn_cmc   hn_cdc1   hn_cdc3   hn_cdc4   hn_gso
                           hn_cabg   hn_ptca  hn_cc     hn_ce     hn_rff    hn_thr
                           hn_tkr    hn_xli   hn_oc     hn_clc    hn_ah     hn_vh
                           hn_p      hn_mast  hn_lump   hn_bs
                           hn_tdis   hn_sdis  hn_mdis   hn_tday   hn_sday   hn_mday
                           hn_opv    hn_erv   hn_asp    hn_ors;


 data bcr0; set cr0;
      cons=1;
      proc sort; by cons;

 data bcr1; set cr1;
      cons=1;
      proc sort; by cons;

data new.cr2007(drop=_FREQ_ _TYPE_ cons); merge bcr0 bcr1; by cons;
     proc print data=new.cr2007(obs=5);

endsas;






